package org.example.skytool.wechat.pay;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.exception.HttpException;
import com.wechat.pay.java.core.exception.MalformedMessageException;
import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.*;
import com.wechat.pay.java.service.payments.model.Transaction;
import static org.example.skytool.wechat.pay.PayConstant.*;


/**
 * @author 22449
 */
public class JsapiService {

    /**
     * 商户号
     */
    public static String merchantId = MCH_ID;
    /**
     * 商户API私钥路径
     */
    public static String privateKeyPath = PRIVATE_KEY_PATH;
    /**
     * 商户证书序列号
     */
    public static String merchantSerialNumber = MCH_SERIAL_NO;
    /**
     * 商户APIV3密钥
     */
    public static String apiV3Key = API_V3KEY;



    public static JsapiServiceExtension service;

    public static Config config;

    static{
        // 初始化商户配置
        config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(merchantId)
                        // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
                        .privateKeyFromPath(privateKeyPath)
                        .merchantSerialNumber(merchantSerialNumber)
                        .apiV3Key(apiV3Key)
                        .build();
        // 初始化服务
        service =
                new JsapiServiceExtension.Builder()
                        .config(config)
                        .signType("RSA") // 不填默认为RSA
                        .build();
    }


    /**
     * 关闭订单
     */
    public static void closeOrder(String tradeNo) {
        CloseOrderRequest request = new CloseOrderRequest();
        // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
        request.setMchid(MCH_ID);
        request.setOutTradeNo(tradeNo);
        // 调用接口
        service.closeOrder(request);
    }

    /**
     * JSAPI支付下单，并返回JSAPI调起支付数据
     */
    public static PrepayWithRequestPaymentResponse prepayWithRequestPayment(String openId,String description,Integer total,String TradeNo) {
        PrepayRequest request = new PrepayRequest();
        // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
        Amount amount = new Amount();
        amount.setTotal(total);
        request.setAmount(amount);
        request.setAppid(APP_ID);
        request.setDescription(description);
        request.setNotifyUrl(PayConstant.NOTIFY_URL);
        request.setOutTradeNo(TradeNo);
        request.setMchid(MCH_ID);
        Payer payer = new Payer();
        payer.setOpenid(openId);
        request.setPayer(payer);
        // 调用接口
        return service.prepayWithRequestPayment(request);
    }

    /**
     * 商户订单号查询订单
     */
    public static Transaction queryOrderByOutTradeNo(String tradeNo) {
        QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
        // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
        request.setMchid(MCH_ID);
        request.setOutTradeNo(tradeNo);
        // 调用接口
        return service.queryOrderByOutTradeNo(request);
    }

}
